home *** CD-ROM | disk | FTP | other *** search
- unit Ccwp;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, ExtCtrls, Buttons, Spin, FileCtrl;
-
- type
- TCCPaperBoySetupDialog = class(TForm)
- Label1: TLabel;
- BitBtn1: TBitBtn;
- BitBtn2: TBitBtn;
- Label2: TLabel;
- Button1: TButton;
- Edit1: TEdit;
- Label3: TLabel;
- OpenDialog1: TOpenDialog;
- CheckBox1: TCheckBox;
- SpinEdit1: TSpinEdit;
- Label4: TLabel;
- RadioGroup1: TRadioGroup;
- BitBtn3: TBitBtn;
- Timer1: TTimer;
- FileListBox1: TFileListBox;
- Image1: TImage;
- procedure BitBtn2Click(Sender: TObject);
- procedure BitBtn3Click(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- procedure RadioGroup1Click(Sender: TObject);
- procedure CheckBox1Click(Sender: TObject);
- procedure SpinEdit1Exit(Sender: TObject);
- procedure Edit1Exit(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Timer1Timer(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FileListBox1Click(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- TheWorkingDirectory : String; { This holds image file dir }
- TimeTillNextDelivery : Integer; { This holds change interval in mins }
- TileTheWallpaper : Integer; { This is the tile setting for windows }
- TheWallpaperFilename : String; { This is the full pathname for win }
- TimeLeftToChange : Integer; { This is the counter for change time }
- SelectionOption : Integer; { 1 = random; 2 = in order selection }
- CurrentSelection : Integer; { This holds the position in the files }
- procedure ReadTheWinIniFile; { This gets the WIN.INI file data }
- procedure WriteTheWinIniFile; { This writes the WIN.INI file data }
- procedure ReadThePaperboyIniFile; { This gets the PAPERBOY INI data }
- procedure WriteThePaperboyIniFile; { This writes the PAPERBOY INI data }
- procedure GetImageFilesInDirectory; { This does ff/fn/fc to get files in d }
- procedure UpdateTheImageDisplay; { This puts an image in the TImage ctl }
- end;
-
- var
- CCPaperBoySetupDialog : TCCPaperBoySetupDialog;
- TheStringList : TStringList; { Used to get around FilLB Bug }
-
- implementation
-
- {$R *.DFM}
- uses
- IniFiles , { Ini file handler unit }
- CCErrors; { Generic error handling unit }
-
- const
-
- PaperBoyIniFileName = 'CCPAPERB.INI'; { Make this a constant to save stack space }
-
- { This updates the combobox when a new directory is chosen or at startup }
- procedure TCCPaperBoySetupDialog.GetImageFilesInDirectory;
- var Counter_1 : Integer;
- begin
- { Clear the current selection }
- CurrentSelection := -1;
- { Put the working directory into the filelistbox }
- FileListBox1.Directory := TheWorkingDirectory;
- { Get an updated listing }
- FileListBox1.Update;
- { Move the strings over to the string list to avoid a bug }
- TheStringList.Assign( FileListbox1.Items );
- { Clear the itemindex to ensure a consistent state }
- FileListBox1.ItemIndex := -1;
- { Run through all the items looking for a match }
- for Counter_1 := 1 to TheStringList.Count do
- begin
- { If match found set current selection and item index }
- if TheStringList.strings[ Counter_1 - 1 ] = TheWallpaperFileName then
- begin
- CurrentSelection := Counter_1;
- FileListBox1.ItemIndex := Counter_1 - 1;
- break;
- end;
- end;
- { If no match found check for any bitmap files present }
- if FileListBox1.ItemIndex = -1 then
- begin
- { If at least one bitmap present make it the default selction }
- if FileListBox1.Items.Count > 0 then
- begin
- TheWallPaperFileName := FileListBox1.Items.Strings[ 0 ];
- CurrentSelection := 0;
- FileListbox1.ItemIndex := 0;
- end
- else
- begin
- { Otherwise have no selection }
- TheWallpaperFileName := '';
- CurrentSelection := -1;
- end;
- end;
- end;
-
- { This encapsulates putting a new image in the display; it will eventually }
- { be used to store a GIF as a bitmap for windows convenience. }
- procedure TCCPaperBoySetupDialog.UpdateTheImageDisplay;
- begin
- { avoid automatic error if no valid filename }
- if TheWallpaperfileName = '' then exit;
- {$I+}
- try { Attempt to load the bitmap file }
- Image1.Picture.LoadFromFile( TheWorkingdirectory + TheWallpaperfilename );
- except
- on E : EInOutError do { If a file error signal it }
- begin
- ErrorDialog( 'Cannot Load ' + TheWallpaperfilename + ' due to '
- + GetIOErrorMessage( E.ErrorCode ));
- end;
- on E : EInvalidGraphic do { If a conversion error signal it }
- begin
- ErrorDialog( 'Cannot Load ' + TheWallpaperFilename + ' due to ' + E.message );
- end;
- end;
- end;
-
- { This procedure reads in the configuration from the WIN INI file }
- procedure TCCPaperBoySetupDialog.ReadTheWinIniFile;
- var
- { The excellent TIniFile object handles the complexity of Windows INI files! }
- TheIniFile : TIniFile;
- begin
- { Combine assignfile and reset into one call with create }
- TheIniFile := TIniFile.Create( 'WIN.INI' );
- { Use a try block to make sure the ini file is closed by free call }
- try
- { Use the Tinifile object's read methods to get the configuration data }
- with TheIniFile do
- begin
- { Use readinteger to get whether to tile the wallpaper }
- TileTheWallpaper := ReadInteger( 'Desktop', 'TileWallpaper', 0 );
- { Use readstring to get the wallpaper filename }
- TheWallpaperFilename := ExtractFileName( ReadString( 'Desktop', 'wallpaper', '' ));
- { Get windows working wallpaper directory in case Paperboy not inited }
- TheWorkingDirectory := ExtractFilePath( ReadString( 'Desktop', 'wallpaper' , '' ));
- end;
- finally
- { Regardless of success or failure, free the TIniFile object }
- TheIniFile.Free;
- end;
- end;
-
- { This procedure writes out the configuration to the WIN INI file }
- procedure TCCPaperBoySetupDialog.WriteTheWinIniFile;
- var
- { Again use the very useful TIniFile object to encapsulate Windows INI files }
- TheIniFile : TIniFile;
- ThePChar : PChar;
- begin
- { Combine an AssignFile and Reset call into the create method }
- TheIniFile := TIniFile.Create( 'WIN.INI' );
- try
- { Use the TIniFile object's methods to write out the data }
- with TheIniFile do
- begin
- { Use writeinteger to send wether to tile the wallpaper }
- WriteInteger( 'Desktop' , 'TileWallpaper' , TileTheWallpaper );
- { Use writestring to send the image file name }
- WriteString( 'Desktop' , 'wallpaper' , TheWorkingDirectory + TheWallpaperFileName );
- end;
- finally
- { Regardless of above calls, free the TIniFile object }
- TheIniFile.Free;
- end;
- { Obtain memory for the windows string }
- GetMem( ThePChar , 255 );
- { Copy the complete pathname into it }
- StrPCopy( ThePChar , TheWorkingDirectory + TheWallpaperFileName );
- { Send the request to windows via arcane API call }
- SystemParametersInfo( SPI_SETDESKWALLPAPER , 0 , ThePChar , SPIF_SENDWININICHANGE );
- { And let the memory go }
- Freemem( ThePChar , 255 );
- end;
-
- { This procedure reads in the configuration from an INI file }
- procedure TCCPaperBoySetupDialog.ReadThePaperBoyIniFile;
- var
- { The excellent TIniFile object handles the complexity of Windows INI files! }
- TheIniFile : TIniFile;
- begin
- { Combine assignfile and reset into one call with create }
- TheIniFile := TIniFile.Create( PaperBoyIniFileName );
- { Use a try block to make sure the ini file is closed by free call }
- try
- { Use the Tinifile object's read methods to get the configuration data }
- with TheIniFile do
- begin
- { Use readinteger to get the interval till the next wallpaper change in min }
- TimeTillNextDelivery := ReadInteger( 'Setup', 'TimeToNextChange', 10 );
- { Use readinteger to get whether to do random or in order image selection }
- SelectionOption := ReadInteger( 'Setup', 'SelectionOption', 1 );
- { Use readstring to get the directory to look for image files }
- { ( Pass in default from WIN.INI in case Paperboy not inited) }
- TheWorkingDirectory := ReadString( 'Setup', 'WorkingDirectory', TheWorkingDirectory );
- end;
- finally
- { Regardless of success or failure, free the TIniFile object }
- TheIniFile.Free;
- end;
- end;
-
- { This procedure writes out the configuration to an INI file }
- procedure TCCPaperBoySetupDialog.WriteThePaperBoyIniFile;
- var
- { Again use the very useful TIniFile object to encapsulate Windows INI files }
- TheIniFile : TIniFile;
- begin
- { Combine an AssignFile and Reset call into the create method }
- TheIniFile := TIniFile.Create( PaperBoyIniFileName );
- try
- { Use the TIniFile object's methods to write out the data }
- with TheIniFile do
- begin
- { Use writeinteger to send the time interval between changes in mins }
- WriteInteger( 'Setup' , 'TimeToNextChange' , TimeTillNextDelivery );
- { Use writeinteger to send whether to use random or in order selection }
- WriteInteger( 'Setup' , 'SelectionOption' , SelectionOption );
- { Use writestring to send the Working directory for image files }
- WriteString( 'Setup' , 'WorkingDirectory' , TheWorkingDirectory );
- end;
- finally
- { Regardless of above calls, free the TIniFile object }
- TheIniFile.Free;
- end;
- end;
-
- { Delphi wrote this; it assumes cancel, so no data sent to windows just hide }
- procedure TCCPaperBoySetupDialog.BitBtn2Click(Sender: TObject);
- begin
- { Hide the setup dialog without saving changes }
- WindowState := wsMinimized;
- end;
-
- { Delphi wrote this; it exits the entire program without saving }
- procedure TCCPaperBoySetupDialog.BitBtn3Click(Sender: TObject);
- begin
- { Don't write the changes; assume just leaving }
- Close;
- end;
-
- { Delphi wrote this; it assumes save data and minimize is the action }
- procedure TCCPaperBoySetupDialog.BitBtn1Click(Sender: TObject);
- begin
- { Write the changes to the WIN INI file }
- WriteTheWinIniFile;
- { Write the changes to the PAPERBOY INI file }
- WriteThePaperBoyIniFile;
- { Hide the setup dialog after saving the changes }
- WindowState := wsMinimized;
- end;
-
- { Delphi wrote this; it sets the internal variable to the radiogroup state }
- procedure TCCPaperBoySetupDialog.RadioGroup1Click(Sender: TObject);
- begin
- { Set the selection option based on the selected radiogroup item }
- SelectionOption := RadioGroup1.ItemIndex + 1;
- end;
-
- { Delphi wrote this; it sets the internal variable to the checkbox state }
- procedure TCCPaperBoySetupDialog.CheckBox1Click(Sender: TObject);
- begin
- { Set the tiling based on whether the box is checked }
- if CheckBox1.Checked then TileTheWallpaper := 1 else
- TileTheWallpaper := 0;
- end;
-
- { Delphi wrote this; it sets the data from the spin edit into the timer var }
- procedure TCCPaperBoySetupDialog.SpinEdit1Exit(Sender: TObject);
- begin
- { If new data entered grab it and make a change }
- if SpinEdit1.Modified then
- begin
- { Reset the delivery interval from the spinedit control }
- TimeTillNextDelivery := StrToInt( SpinEdit1.Text );
- end;
- end;
-
- { Delphi wrote this; it checks on exit of the edit control for a change }
- { and updates the system if one was made. }
- procedure TCCPaperBoySetupDialog.Edit1Exit(Sender: TObject);
- begin
- { If something has been entered grab it }
- if Edit1.Modified then
- begin
- { provided the chosen wallpaper exists get it and its dir }
- if FileExists( Edit1.Text ) then
- begin
- { Get the new working directory }
- TheWorkingDirectory := ExtractFilePath( Edit1.Text );
- { Extract the filename for sending to windows }
- TheWallPaperFilename := ExtractFileName( Edit1.Text );
- { Get the list of image files and put in combobox }
- GetImageFilesInDirectory;
- { Put in a preview just for fun }
- UpdateTheImageDisplay;
- end;
- end;
- end;
-
- { Delphi wrote this; it runs an OpenDialog and then updates from the result }
- procedure TCCPaperBoySetupDialog.Button1Click(Sender: TObject);
- begin
- { If the open dialog isn't cancelled then get the info }
- if OpenDialog1.Execute then
- begin
- Edit1.Text := OpenDialog1.FileName;
- { provided the chosen wallpaper exists get it and its dir }
- if FileExists( Edit1.Text ) then
- begin
- { Get the new working directory }
- TheWorkingDirectory := ExtractFilePath( Edit1.Text );
- { Extract the filename for sending to windows }
- TheWallPaperFilename := ExtractFileName( Edit1.Text );
- { Get the list of image files and put in combobox }
- GetImageFilesInDirectory;
- { Put in a preview just for fun }
- UpdateTheImageDisplay;
- end;
- end;
- end;
-
- { Delphi wrote this; it is the actual routine to update the wallpaper }
- procedure TCCPaperBoySetupDialog.Timer1Timer(Sender: TObject);
- begin
- { abort if no valid selection }
- if CurrentSelection = -1 then exit;
- { Decrease the available time by 1 minute }
- TimeLeftToChange := TimeLeftToChange - 1;
- { If the time has run out, do a wallpaper change }
- if TimeLeftToChange = 0 then
- begin
- { Do either a random or an inorder change }
- case SelectionOption of
- 1 : begin { Select a new wallpaper randomly }
- if FileListbox1.ItemIndex <> -1 then { If there are no items don't change }
- begin
- { Get a random number equal to 1 to total items in list }
- CurrentSelection := Random( TheStringList.Count ) + 1;
- { Get the filename from the combobox list }
- TheWallPaperFileName := TheStringList.Strings[ CurrentSelection - 1 ];
- { Send the info out to windows }
- WriteTheWinIniFile;
- { And reset the counter back to start for next loop }
- TimeLeftToChange := TimeTillNextDelivery;
- end;
- end;
- 2 : begin { Move down 1 and then loop back to top }
- { If there are no items listed then exit }
- if FileListbox1.Items.Count > 0 then
- begin
- { Otherwise bump the selection up one }
- CurrentSelection := CurrentSelection + 1;
- { If past the maximum entries then loop back to 1 }
- if CurrentSelection > TheStringList.Count then
- CurrentSelection := 1;
- { Get the filename from the combobox }
- TheWallPaperFileName := TheStringList.Strings[ CurrentSelection - 1 ];
- { Tell windows about the changes }
- WriteTheWinIniFile;
- { And reset the counter for next time }
- TimeLeftToChange := TimeTillNextDelivery;
- end;
- end;
- end;
- end;
- end;
-
- { Delphi wrote this; it is used to create all the data on startup }
- procedure TCCPaperBoySetupDialog.FormCreate(Sender: TObject);
- begin
- { Seed the random number generator }
- Randomize;
- { Create the string list }
- TheStringList := TStringList.Create;
- { Get the windows data }
- ReadTheWinIniFile;
- { Get the paperboy data }
- ReadThePaperboyIniFile;
- { Set the checkbox for tiled wallpaper }
- CheckBox1.Checked := ( TileTheWallpaper = 1 );
- { Set the radiogroup to the option selected }
- RadioGroup1.ItemIndex := SelectionOption - 1;
- { Set the SpinEdit to the read in time value }
- SpinEdit1.Text := IntToStr( TimeTillNextDelivery );
- { Set the Working Directory to the imported value }
- Edit1.Text := TheWorkingDirectory;
- { Clear the time left timer }
- TimeLeftToChange := TimeTillNextDelivery;
- { Get the list of image files and put in combobox }
- GetImageFilesInDirectory;
- { Put in a preview just for fun }
- UpdateTheImageDisplay;
- end;
-
- { Delphi wrote this; it sets the new wallpaper filename to the selection }
- { and updates the preview display }
- procedure TCCPaperBoySetupDialog.FileListBox1Click(Sender: TObject);
- begin
- { If there is something selected in the FLB make it new filename }
- if FileListbox1.ItemIndex <> -1 then
- TheWallPaperFileName := FileListbox1.Items.Strings[ FileListbox1.ItemIndex ];
- { Update the preview display }
- UpdateTheImageDisplay;
- end;
-
- { Delphi wrote this; put in code to free the stringlist to save memory! }
- procedure TCCPaperBoySetupDialog.FormDestroy(Sender: TObject);
- begin
- { Free the string list }
- TheStringList.Free;
- end;
-
- end.
-